package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {


    @Autowired
    private WechatService wechatService;

    @Autowired
    private TokenService tokenService;

    private static List<String> NICK_NAME_PREFIX_LIST = Arrays.asList(
            "钱途无量",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝");

    /**
     * 微信登录
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        //1. 获取用户openId
        String openId = wechatService.getOpenId(dto.getCode());
        if (StringUtils.isBlank(openId)) {
            throw new BaseException("登录失败");
        }

        //2. 判断用户是否存在，不存在则创建
        FamilyMember member = this.lambdaQuery().eq(FamilyMember::getOpenId, openId).one();
        if (member == null) {
            member = FamilyMember.builder().openId(openId).build();

            Collections.shuffle(NICK_NAME_PREFIX_LIST);//打乱集合中元素顺序，目的是随机获取元素

//            // 获取完整手机号
//            String phone = wechatService.getPhone(dto.getPhoneCode());
//            // 截取手机号后四位
//            String lastFourDigits = phone.substring(phone.length() - 4);

            // 设置 name 为随机获取的元素加上手机号后四位
            member.setName(NICK_NAME_PREFIX_LIST.get(0) );
            this.save(member);
        }
        //3. 判断手机号是否存在，不存在则根据openId获取用户手机号
        if (StringUtils.isBlank(member.getPhone())) {
            String phone = wechatService.getPhone(dto.getPhoneCode());
            if (StringUtils.isNotBlank(phone)) {
            }
            //4. 获取到手机号，就更新用户信息
            FamilyMember updateMember = FamilyMember.builder()
                    .id(member.getId())
                    .phone(phone)
                    .build();
            this.updateById(updateMember);
        }
        //5. 生成令牌
        Map<String, Object> cliamMap = new HashMap<>();
        cliamMap.put("userId", member.getId());
        cliamMap.put("nickName", member.getName());

        String token = tokenService.createToken(cliamMap);

        //6. 返回数据
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(member.getName()+member.getPhone().substring(member.getPhone().length()-4));
        return loginVo;
    }
}
